﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib;
using System.Data.OleDb;

namespace wap主题
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Windows.Forms.Button[] btnCOMs;
        System.Windows.Forms.TextBox[] txCOMs;
        SerialPort[] COMs;
        public List<主题Data> Tasks;
        private void Form1_Load(object sender, EventArgs e)
        {
            WFNetLib.ADO.AccessOP.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=主题.mdb";
            btnCOMs = new System.Windows.Forms.Button[16];
            txCOMs = new System.Windows.Forms.TextBox[16];
            COMs = new SerialPort[16];
            for (int i = 0; i < 16; i++)
            {
                btnCOMs[i] = new System.Windows.Forms.Button();
                txCOMs[i] = new System.Windows.Forms.TextBox();
            }
            splitContainer8.Panel1.Controls.Add(txCOMs[0]);
            splitContainer8.Panel2.Controls.Add(txCOMs[1]);
            splitContainer9.Panel1.Controls.Add(txCOMs[2]);
            splitContainer9.Panel2.Controls.Add(txCOMs[3]);
            splitContainer10.Panel1.Controls.Add(txCOMs[4]);
            splitContainer10.Panel2.Controls.Add(txCOMs[5]);
            splitContainer11.Panel1.Controls.Add(txCOMs[6]);
            splitContainer11.Panel2.Controls.Add(txCOMs[7]);
            splitContainer12.Panel1.Controls.Add(txCOMs[8]);
            splitContainer12.Panel2.Controls.Add(txCOMs[9]);
            splitContainer13.Panel1.Controls.Add(txCOMs[10]);
            splitContainer13.Panel2.Controls.Add(txCOMs[11]);
            splitContainer14.Panel1.Controls.Add(txCOMs[12]);
            splitContainer14.Panel2.Controls.Add(txCOMs[13]);
            splitContainer15.Panel1.Controls.Add(txCOMs[14]);
            splitContainer15.Panel2.Controls.Add(txCOMs[15]);

            splitContainer8.Panel1.Controls.Add(btnCOMs[0]);
            splitContainer8.Panel2.Controls.Add(btnCOMs[1]);
            splitContainer9.Panel1.Controls.Add(btnCOMs[2]);
            splitContainer9.Panel2.Controls.Add(btnCOMs[3]);
            splitContainer10.Panel1.Controls.Add(btnCOMs[4]);
            splitContainer10.Panel2.Controls.Add(btnCOMs[5]);
            splitContainer11.Panel1.Controls.Add(btnCOMs[6]);
            splitContainer11.Panel2.Controls.Add(btnCOMs[7]);
            splitContainer12.Panel1.Controls.Add(btnCOMs[8]);
            splitContainer12.Panel2.Controls.Add(btnCOMs[9]);
            splitContainer13.Panel1.Controls.Add(btnCOMs[10]);
            splitContainer13.Panel2.Controls.Add(btnCOMs[11]);
            splitContainer14.Panel1.Controls.Add(btnCOMs[12]);
            splitContainer14.Panel2.Controls.Add(btnCOMs[13]);
            splitContainer15.Panel1.Controls.Add(btnCOMs[14]);
            splitContainer15.Panel2.Controls.Add(btnCOMs[15]);


            for (int i = 0; i < 16; i++)
            {
                btnCOMs[i].Dock = DockStyle.Top;
                btnCOMs[i].Name = "btnCOM" + (i + 3).ToString();
                btnCOMs[i].Text = "COM" + (i + 3).ToString() + "---等待";
                btnCOMs[i].UseVisualStyleBackColor = true;
                btnCOMs[i].Click += new System.EventHandler(btnClick);
                btnCOMs[i].Tag = i;
                btnCOMs[i].BackColor = Color.Green;
                btnCOMs[i].Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                btnCOMs[i].Height = 40;

                txCOMs[i].Dock = DockStyle.Fill;
                txCOMs[i].Multiline = true;
                txCOMs[i].Name = "txCOM" + (i + 3).ToString();
                txCOMs[i].ScrollBars = System.Windows.Forms.ScrollBars.Vertical;


                COMs[i] = new SerialPort("COM" + (i + 3).ToString(), 115200);
                //COMs[i].Handshake = Handshake.RequestToSend;
                //COMs[i].DtrEnable = true;
                //COMs[i].PortName = "COM18";
            }
            ReaderThread.AddLogEvent += new WFNetLib.strEventHandler(ReaderThread_AddLogEvent);
            ReaderThread.FinishEvent += new intEventHandler(ReaderThread_FinishEvent);
            OleDbDataReader read = WFNetLib.ADO.AccessOP.ReaderExecuteSQL("select * from 主题");
            Tasks = new List<主题Data>();
            while (read.Read())
            {
                Tasks.Add(主题DataFactory.Construct(read));
            }
        }
        private void btnClick(object sender, EventArgs e)
        {
            int btnInex = (int)((System.Windows.Forms.Button)sender).Tag;
            if (btnCOMs[btnInex].BackColor == Color.Yellow)//等待中
                return;
            else
            {
                txCOMs[btnInex].Clear();
                Tasks = new List<主题Data>();
                OleDbDataReader reader = WFNetLib.ADO.AccessOP.ReaderExecuteSQL("select * from 主题");
                while (reader.Read())
                {
                    Tasks.Add(主题DataFactory.Construct(reader));
                }
                ReaderThread read = new ReaderThread(COMs[btnInex], btnInex,Tasks);
                read.ReaderThreadStart();
                btnCOMs[btnInex].BackColor = Color.Yellow;
                btnCOMs[btnInex].Text = "COM" + (btnInex + 3).ToString() + "---查询中";
            }
        }
        void ReaderThread_AddLogEvent(object sender, strEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                ReaderThread read = (ReaderThread)sender;
                txCOMs[read.ComIndex].AppendText(e.strArg + "\r\n");
            }));
        }
        void ReaderThread_FinishEvent(object sender, intEventArgs e)
        {
            this.Invoke((EventHandler)(delegate
            {
                ReaderThread read = (ReaderThread)sender;
                FinishType ft = (FinishType)e.nArg;
                switch (ft)
                {
                    case FinishType.Finish:
                        btnCOMs[read.ComIndex].Text = "COM" + (read.ComIndex + 3).ToString() + "---所有任务已经完成";
                        btnCOMs[read.ComIndex].BackColor = Color.Green;
                        break;
                    case FinishType.DoAgain:
                        btnCOMs[read.ComIndex].Text = "COM" + (read.ComIndex + 3).ToString() + "---1天只允许签到一次";
                        btnCOMs[read.ComIndex].BackColor = Color.Green;
                        break;
                    case FinishType.Unkown:
                        btnCOMs[read.ComIndex].Text = "COM" + (read.ComIndex + 3).ToString() + "---网页连接错误，请重新开始任务";
                        btnCOMs[read.ComIndex].BackColor = Color.Red;
                        break;
                    case FinishType.Error:
                        btnCOMs[read.ComIndex].Text = "COM" + (read.ComIndex + 3).ToString() + "---当前通道有任务未完成";
                        btnCOMs[read.ComIndex].BackColor = Color.Red;
                        break;
                }
            }));
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 16; i++)
            {
                btnClick(btnCOMs[i], new EventArgs());
            }
        }
    }
}
